package org.footito.android.data.database;

import org.footito.android.R;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;

public class FootitoProvider extends ContentProvider {
	private static final String TAG = "FootitoProvider";
	private static final String CONTENT = "content://";
	private static final String AUTHORITY = "org.footito.android.data";

	public static final String URI_EVENTS = CONTENT + AUTHORITY + "/events";
	public static final String URI_COMMENTS = CONTENT + AUTHORITY + "/comments";
	public static final String URI_SUBSCRIBERS = CONTENT + AUTHORITY + "/subscribers";
	public static final String URI_TEAMS = CONTENT + AUTHORITY + "/teams";

	public static final Uri CONTENT_URI_EVENTS = Uri.parse(URI_EVENTS);
	public static final Uri CONTENT_URI_COMMENTS = Uri.parse(URI_COMMENTS);
	public static final Uri CONTENT_URI_SUBSCRIBERS = Uri.parse(URI_SUBSCRIBERS);
	public static final Uri CONTENT_URI_TEAMS = Uri.parse(URI_TEAMS);

	public static final int COLNUM_EVENT_ID = DatabaseAdapter.COLNUM_EVENT_ID;
	public static final int COLNUM_EVENT_TYPE = DatabaseAdapter.COLNUM_EVENT_TYPE;
	public static final int COLNUM_EVENT_IDTEAM = DatabaseAdapter.COLNUM_EVENT_IDTEAM;
	public static final int COLNUM_EVENT_TIMESTAMP = DatabaseAdapter.COLNUM_EVENT_TIMESTAMP;
	public static final int COLNUM_EVENT_CONTENT = DatabaseAdapter.COLNUM_EVENT_CONTENT;
	public static final int COLNUM_EVENT_LINK = DatabaseAdapter.COLNUM_EVENT_LINK;
	public static final int COLNUM_EVENT_INSERTION = DatabaseAdapter.COLNUM_EVENT_INSERTION;

	public static final int COLNUM_SUBSCRIBER_ID = DatabaseAdapter.COLNUM_SUBSCRIBER_ID;
	public static final int COLNUM_SUBSCRIBER_IDEVENT = DatabaseAdapter.COLNUM_SUBSCRIBER_IDEVENT;
	public static final int COLNUM_SUBSCRIBER_IDTEAM = DatabaseAdapter.COLNUM_SUBSCRIBER_IDTEAM;

	public static final int COLNUM_TEAM_ID = DatabaseAdapter.COLNUM_TEAM_ID;
	public static final int COLNUM_TEAM_NAME = DatabaseAdapter.COLNUM_TEAM_NAME;
	public static final int COLNUM_TEAM_LINK = DatabaseAdapter.COLNUM_TEAM_LINK;
	public static final int COLNUM_TEAM_PICTURE = DatabaseAdapter.COLNUM_TEAM_PICTURE;

	public static final int COLNUM_COMMENT_ID = DatabaseAdapter.COLNUM_COMMENT_ID;
	public static final int COLNUM_COMMENT_IDEVENT = DatabaseAdapter.COLNUM_COMMENT_IDEVENT;
	public static final int COLNUM_COMMENT_CONTENT = DatabaseAdapter.COLNUM_COMMENT_CONTENT;
	public static final int COLNUM_COMMENT_IDTEAM = DatabaseAdapter.COLNUM_COMMENT_IDTEAM;

	private static final String ONE_EVENT_TYPE = "vdn.android.cursor.item/event";
	private static final String ALL_EVENTS_TYPE = "vdn.android.cursor.collection/event";
	private static final String ONE_TEAM_TYPE = "vdn.android.cursor.item/team";
	private static final String ALL_TEAMS_TYPE = "vdn.android.cursor.collection/team";
	private static final String ONE_SUBSCRIBER_TYPE = "vdn.android.cursor.item/subscriber";
	private static final String ALL_SUBSCRIBERS_TYPE = "vdn.android.cursor.collection/subscriber";
	private static final String ONE_COMMENT_TYPE = "vdn.android.cursor.item/comment";
	private static final String ALL_COMMENTS_TYPE = "vdn.android.cursor.collection/comment";

	private static final int ALL_EVENTS = 0;
	private static final int ONE_EVENT = 1;
	private static final int ALL_TEAMS = 2;
	private static final int ONE_TEAM = 3;
	private static final int ALL_SUBSCRIBERS = 4;
	private static final int ONE_SUBSCRIBER = 5;
	private static final int ALL_COMMENTS = 6;
	private static final int ONE_COMMENT = 7;

	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, "events", ALL_EVENTS);
		uriMatcher.addURI(AUTHORITY, "events/#", ONE_EVENT);
		uriMatcher.addURI(AUTHORITY, "teams", ALL_TEAMS);
		uriMatcher.addURI(AUTHORITY, "teams/#", ONE_TEAM);
		uriMatcher.addURI(AUTHORITY, "subscribers", ALL_SUBSCRIBERS);
		uriMatcher.addURI(AUTHORITY, "subscribers/#", ONE_SUBSCRIBER);
		uriMatcher.addURI(AUTHORITY, "comments", ALL_COMMENTS);
		uriMatcher.addURI(AUTHORITY, "comments/#", ONE_COMMENT);
	}
	private DatabaseAdapter dbAdapter;

	public FootitoProvider() {
		super();
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int retVal = 0;
		int id;

		switch (uriMatcher.match(uri)) {
		case ONE_EVENT:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
				retVal = dbAdapter.removeEvent(id);
			} catch (Exception e) {
				throw new IllegalArgumentException(getContext().getResources().getString(
						R.string.errorUriNotSuporter)
						+ uri);
			}
			break;
		case ALL_EVENTS:
			retVal = dbAdapter.removeEvents(selection, selectionArgs);
			break;

		case ONE_SUBSCRIBER:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
				retVal = dbAdapter.removeSubscriber(id);
			} catch (Exception e) {
				throw new IllegalArgumentException(getContext().getResources().getString(
						R.string.errorUriNotSuporter)
						+ uri);
			}
			break;
		case ALL_SUBSCRIBERS:
			retVal = dbAdapter.removeSubscribers(selection, selectionArgs);
			break;

		case ONE_COMMENT:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
				retVal = dbAdapter.removeComment(id);
			} catch (Exception e) {
				throw new IllegalArgumentException(getContext().getResources().getString(
						R.string.errorUriNotSuporter)
						+ uri);
			}
			break;
		case ALL_COMMENTS:
			retVal = dbAdapter.removeComments(selection, selectionArgs);
			break;

		case ONE_TEAM:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
				retVal = dbAdapter.removeTeam(id);
			} catch (Exception e) {
				throw new IllegalArgumentException(getContext().getResources().getString(
						R.string.errorUriNotSuporter)
						+ uri);
			}
			break;
		case ALL_TEAMS:
			retVal = dbAdapter.removeTeams(selection, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException(getContext().getResources().getString(
					R.string.errorUriNotSuporter)
					+ uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return retVal;
	}

	@Override
	public String getType(Uri uri) {
		switch (uriMatcher.match(uri)) {
		case ONE_EVENT:
			return ONE_EVENT_TYPE;
		case ALL_EVENTS:
			return ALL_EVENTS_TYPE;
		case ONE_COMMENT:
			return ONE_COMMENT_TYPE;
		case ALL_COMMENTS:
			return ONE_COMMENT_TYPE;
		case ONE_SUBSCRIBER:
			return ONE_SUBSCRIBER_TYPE;
		case ALL_SUBSCRIBERS:
			return ONE_SUBSCRIBER_TYPE;
		case ONE_TEAM:
			return ONE_TEAM_TYPE;
		case ALL_TEAMS:
			return ONE_TEAM_TYPE;

		default:
			throw new IllegalArgumentException(getContext().getResources().getString(
					R.string.errorUriNotSuporter)
					+ uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		long id;
		switch (uriMatcher.match(uri)) {
		case ONE_EVENT:
		case ALL_EVENTS:
			id = dbAdapter.insertEvent(values);
			if (id > 0) {
				Uri retUri = ContentUris.withAppendedId(CONTENT_URI_EVENTS, id);
				getContext().getContentResolver().notifyChange(retUri, null);
				return retUri;
			}
		case ONE_COMMENT:
		case ALL_COMMENTS:
			id = dbAdapter.insertComment(values);
			if (id > 0) {
				Uri retUri = ContentUris.withAppendedId(CONTENT_URI_COMMENTS, id);
				getContext().getContentResolver().notifyChange(retUri, null);
				return retUri;
			}

		case ONE_SUBSCRIBER:
		case ALL_SUBSCRIBERS:
			id = dbAdapter.insertSubscriber(values);
			if (id > 0) {
				Uri retUri = ContentUris.withAppendedId(CONTENT_URI_SUBSCRIBERS, id);
				getContext().getContentResolver().notifyChange(retUri, null);
				return retUri;
			}

		case ONE_TEAM:
		case ALL_TEAMS:
			id = dbAdapter.insertTeam(values);
			if (id > 0) {
				Uri retUri = ContentUris.withAppendedId(CONTENT_URI_TEAMS, id);
				getContext().getContentResolver().notifyChange(retUri, null);
				return retUri;
			}

		default:
			throw new IllegalArgumentException(getContext().getResources().getString(
					R.string.errorUriNotSuporter)
					+ uri);
		}
	}

	@Override
	public boolean onCreate() {
		dbAdapter = new DatabaseAdapter(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
			String sortOrder) {
		int id;
		switch (uriMatcher.match(uri)) {
		case ONE_EVENT:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
			} catch (Exception e) {
				return null;
			}

			Cursor retCursorEvent = dbAdapter.getEventCursor(id);
			retCursorEvent.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorEvent;

		case ALL_EVENTS:
			Cursor retCursorEvents = dbAdapter.getAllEventsCursor(projection, selection,
					selectionArgs, sortOrder);
			retCursorEvents.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorEvents;

		case ONE_SUBSCRIBER:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
			} catch (Exception e) {
				return null;
			}

			Cursor retCursorSubscriber = dbAdapter.getSubscriberCursor(id);
			retCursorSubscriber.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorSubscriber;

		case ALL_SUBSCRIBERS:
			Cursor retCursorSubscribers = dbAdapter.getAllSubscribersCursor(projection, selection,
					selectionArgs, sortOrder);
			retCursorSubscribers.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorSubscribers;

		case ONE_COMMENT:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
			} catch (Exception e) {
				return null;
			}

			Cursor retCursorComment = dbAdapter.getCommentCursor(id);
			retCursorComment.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorComment;

		case ALL_COMMENTS:
			Cursor retCursorComments = dbAdapter.getAllCommentsCursor(projection, selection,
					selectionArgs, sortOrder);
			retCursorComments.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorComments;

		case ONE_TEAM:
			id = 0;
			try {
				id = Integer.parseInt(uri.getPathSegments().get(1));
			} catch (Exception e) {
				return null;
			}

			Cursor retCursorTeam = dbAdapter.getTeamCursor(id);
			retCursorTeam.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorTeam;

		case ALL_TEAMS:
			Cursor retCursorTeams = dbAdapter.getAllTeamsCursor(projection, selection,
					selectionArgs, sortOrder);
			retCursorTeams.setNotificationUri(getContext().getContentResolver(), uri);
			return retCursorTeams;

		default:
			break;
		}
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		int retVal = 0;
		String id;
		switch (uriMatcher.match(uri)) {
		case ONE_EVENT:
			id = uri.getPathSegments().get(1);
			retVal = dbAdapter.updateEvent(values, DatabaseAdapter.COLNUM_EVENT_ID + " = " + id,
					selectionArgs);
			break;
		case ALL_EVENTS:
			retVal = dbAdapter.updateEvent(values, selection, selectionArgs);
			break;

		case ONE_SUBSCRIBER:
			id = uri.getPathSegments().get(1);
			retVal = dbAdapter.updateSubscriber(values, DatabaseAdapter.COLNUM_SUBSCRIBER_ID
					+ " = " + id, selectionArgs);
			break;
		case ALL_SUBSCRIBERS:
			retVal = dbAdapter.updateSubscriber(values, selection, selectionArgs);
			break;

		case ONE_COMMENT:
			id = uri.getPathSegments().get(1);
			retVal = dbAdapter.updateComment(values,
					DatabaseAdapter.COLNUM_COMMENT_ID + " = " + id, selectionArgs);
			break;
		case ALL_COMMENTS:
			retVal = dbAdapter.updateComment(values, selection, selectionArgs);
			break;

		case ONE_TEAM:
			id = uri.getPathSegments().get(1);
			retVal = dbAdapter.updateTeam(values, DatabaseAdapter.COLNUM_TEAM_ID + " = " + id,
					selectionArgs);
			break;
		case ALL_TEAMS:
			retVal = dbAdapter.updateTeam(values, selection, selectionArgs);
			break;

		default:
			throw new IllegalArgumentException(getContext().getResources().getString(
					R.string.errorUriNotSuporter)
					+ uri);
		}
		getContext().getContentResolver().notifyChange(uri, null);
		return retVal;
	}

}
